<?php
require_once '../class/control.class.php';

include 'ckeditor/ckeditor.php';
$CKEditor = new CKEditor();
$CKEditor->returnOutput = true;
$CKEditor->basePath = 'ckeditor/';
$CKEditor->config['width'] = 750;
$CKEditor->config['language'] = "pt-br";
$CKEditor->config['toolbar'] = array(
	 array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),
	 array( 'Image', 'Link', 'Unlink', 'Anchor' ));
$CKEditor->textareaAttributes = array("cols" => 80, "rows" => 10);

if($_GET['editar']){
	$obj = new Control('consultar','artigos',$_GET['editar']);
	$obj->Consult();
	$con = $obj->result->lista;
}
?>
<div id="load"><img src="imagens/wait.gif" /></div>
<div><span id="efeito" >Cadastrar</span>
	<div id="box1">
		<form id="formArtigos" method="post" enctype="multipart/form-data"  >
			<table width="80%" border="0" cellspacing="5" cellpadding="0" align="center">
				<tr>
					<td>Titulo:</td>
					<td><input name="titulo" id="titulo" type="text" class="input" value="<?php echo $_GET['editar'] ? $con['titulo'] : ''; ?>" /></td>
					<td></td>
				</tr>
				<tr>
					<td>Autor:</td>
					<td><input name="autor" id="autor" type="text" class="input" value="<?php echo $_GET['editar'] ? $con['autor'] : ''; ?>" /></td>
					<td></td>
				</tr>
				<tr>
					<td>Data:</td>
					<td><input name="data" id="data" type="text" class="input" value="<?php echo $_GET['editar'] ? date('d/m/Y',strtotime($con['data'])) : date('d/m/Y'); ?>" /></td>
					<td></td>
				</tr>
				<tr valign="top">
					<td>Resumo:</td>	
					<td colspan="2">	
						<textarea name="resumo" id="resumo" class="textarea" ><?php echo $_GET['editar'] ? $con['resumo'] : ''; ?></textarea>	
					</td>
				</tr>
				<tr>
					<td colspan="3">Texto:</td>
				</tr>
				<tr>	
					<td colspan="3">	
						<?php echo $CKEditor->editor("texto", $con['texto']); ?>	
					</td>
				</tr>
				<tr>
					<td>Imagem:</td>
					<td><input name="imagem0" id="imagem0" type="file" class="input" /></td>
					<td></td>
				</tr>
				<?php if($_GET['editar']){ ?>
				<tr valign="top">
					<td>Imagem atual:</td>
					<td>
						<?php echo strlen($con['imagem0']) > 3 ? '<img src="../imagens/artigos/'.$con['imagem0'].'" width="100" />' : ''; ?>
					</td>
					<td>&nbsp;</td>
				</tr>
				<?php } ?>
				<tr align="right">
					<td colspan="3">
						<input type="hidden" name="pagina" 	id="pagina" value="Artigos" />
						<?php 
						if($_GET['editar']){
							echo '<input type="hidden" name="acao" 				id="acao" 			value="update" />';
							echo '<input type="hidden" name="idartigos"		id="idartigos"		value="',$_GET['editar'],'" />';
						}else{
							echo '<input type="hidden" name="acao" id="acao" value="insert" />';
						}
						?>	
						<input type="submit" value="<?php echo $_GET['editar'] ? 'Atualizar' : 'Cadastrar'; ?>" />
					</td>
				</tr>
			</table>
		</form>
	</div>
	<span id="efeito" >Listar / Editar / Excluir Cadastrados</span>
	<div id="box2">
		<table id="tableOne" class="yui">
			<thead>
				<tr>
					<th>T&iacute;tulo</th>
					<th>Data</th>
					<th>&nbsp;</th>
					<th>&nbsp;</th>
				</tr>
			</thead>
			<tbody>
				<?php
					$lst = new Control('listar','artigos','');
					$lst->Lister();
					echo $lst->result->lista;
				?>
			</tbody>
		</table>
	</div>
	<script type="text/javascript"> 
        $(document).ready(function() { 
        	var options = { 
        			beforeSubmit:  loading,
        		    success:    sucesso,
        		    url:    '../class/server.php'
        		};
            $('#formArtigos').ajaxForm(options);
            $('#data').mask('99/99/9999');
            $("#tableOne").tablesorter({ sortList: [[0, 0,]], widgets: ['zebra'] });
        }); 
    </script>
</div>